<template>
  <el-dialog
    v-model="visible"
    width="720px"
    top="8vh"
    :append-to-body="true"
    :close-on-click-modal="false"
    class="unified-dialog jar-detail-dialog"
    @close="handleClose"
  >
    <template #header>
      <DialogHeader
        icon="Document"
        :title="dialogTitle"
        description="查看JAR包详细信息"
        type="primary"
      />
    </template>

    <div v-if="jarData" class="jar-detail-content">
      <!-- 基础信息 -->
      <div class="info-section">
        <div class="section-title">
          <el-icon><Document /></el-icon>
          <span>基础信息</span>
        </div>
        
        <div class="info-grid">
          <div class="info-item">
            <label>JAR名称</label>
            <div class="info-value copyable" @click="copyToClipboard(jarData.name, 'JAR名称')">
              <span>{{ jarData.name }}</span>
              <el-icon class="copy-icon"><Document /></el-icon>
            </div>
          </div>
          
          <div class="info-item">
            <label>JAR ID</label>
            <div class="info-value copyable" @click="copyToClipboard(jarData.id, 'JAR ID')">
              <span>{{ jarData.id }}</span>
              <el-icon class="copy-icon"><Document /></el-icon>
            </div>
          </div>
          
          <div class="info-item">
            <label>上传时间</label>
            <div class="info-value">
              <span>{{ formatTime(jarData.uploaded) }}</span>
            </div>
          </div>
          
        </div>
      </div>

      <!-- 入口类信息 -->
      <div v-if="jarData.entry" class="info-section">
        <div class="section-title">
          <el-icon><Star /></el-icon>
          <span>入口类信息</span>
        </div>
        
        <div class="info-item full-width">
          <label>主类</label>
          <div class="info-value copyable" @click="copyToClipboard(jarData.entry, '入口类')">
            <span>{{ jarData.entry }}</span>
            <el-icon class="copy-icon"><Document /></el-icon>
          </div>
        </div>
      </div>

      <!-- 程序参数 -->
      <div v-if="jarData.programArgs" class="info-section">
        <div class="section-title">
          <el-icon><Setting /></el-icon>
          <span>程序参数</span>
        </div>
        
        <div class="info-item full-width">
          <label>参数</label>
          <div class="info-value copyable" @click="copyToClipboard(jarData.programArgs, '程序参数')">
            <span>{{ jarData.programArgs }}</span>
            <el-icon class="copy-icon"><Document /></el-icon>
          </div>
        </div>
      </div>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
        <el-button type="danger" @click="handleDelete" :loading="isDeleting">
          <el-icon><Delete /></el-icon>
          删除JAR包
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, computed } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { componentLogger } from '@/utils/logger'
import { copyToClipboard as copyText } from '@/utils/clipboard'
import dayjs from 'dayjs'
import DialogHeader from '@/components/DialogHeader.vue'

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  jar: {
    type: Object,
    default: () => ({})
  }
})

const emit = defineEmits(['update:modelValue', 'delete'])

const visible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

const dialogTitle = computed(() => {
  return props.jar?.name ? `JAR包详情 - ${props.jar.name}` : 'JAR包详情'
})

const jarData = computed(() => props.jar)

const isDeleting = ref(false)

const formatTime = (time) => {
  if (!time) return '-'
  return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
}


// 复制到剪贴板 - 使用统一工具
const copyToClipboard = async (text, fieldName) => {
  await copyText(text, fieldName)
}

const handleClose = () => {
  visible.value = false
}


const handleDelete = async () => {
  try {
    await ElMessageBox.confirm(
      `确定要删除JAR包 "${jarData.value.name}" 吗？此操作不可恢复！`,
      '确认删除',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }
    )
    
    isDeleting.value = true
    emit('delete', jarData.value)
  } catch (error) {
    if (error !== 'cancel') {
      componentLogger.error('删除失败:', error)
      ElMessage.error('删除失败')
    }
  } finally {
    isDeleting.value = false
  }
}
</script>

<style scoped>
/* 对话框已使用统一样式系统 (dialog-system.css) */

.jar-detail-dialog .el-dialog__body {
  max-height: 600px;
  overflow-y: auto;
}

.jar-detail-content {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.info-section {
  padding: 20px;
  background: #f8f9fa;
  border-radius: 12px;
  border: 1px solid #e4e7ed;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.info-section:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.section-title {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 2px solid #409eff;
  color: #303133;
  font-size: 16px;
  font-weight: 600;
}

.section-title .el-icon {
  margin-right: 8px;
  font-size: 18px;
  color: #409eff;
  background: rgba(64, 158, 255, 0.1);
  padding: 8px;
  border-radius: 8px;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 16px;
}

.info-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.info-item.full-width {
  grid-column: 1 / -1;
}

.info-item label {
  font-size: 13px;
  font-weight: 600;
  color: #606266;
  margin: 0;
}

.info-value {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: #fff;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  font-size: 14px;
  color: #303133;
  transition: all 0.3s ease;
}

.info-value.copyable {
  cursor: pointer;
  position: relative;
}

.info-value.copyable:hover {
  background: rgba(64, 158, 255, 0.05);
  border-color: #409eff;
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(64, 158, 255, 0.1);
}

.info-value span {
  flex: 1;
  word-break: break-all;
}

.copy-icon {
  margin-left: 8px;
  opacity: 0;
  transition: all 0.3s ease;
  color: #409eff;
  font-size: 16px;
}

.info-value.copyable:hover .copy-icon {
  opacity: 1;
}

/* 滚动条样式 */
.jar-detail-dialog .el-dialog__body::-webkit-scrollbar {
  width: 6px;
}

.jar-detail-dialog .el-dialog__body::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

.jar-detail-dialog .el-dialog__body::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

.jar-detail-dialog .el-dialog__body::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .jar-detail-dialog {
    width: 95% !important;
    margin: 0 auto;
  }
  
  .info-grid {
    grid-template-columns: 1fr;
  }
}
</style>
